package com.zycfc.zsf.boot.office.excel.service;

import com.zycfc.zsf.boot.office.excel.bean.*;
import com.zycfc.zsf.boot.office.excel.util.*;
import com.zycfc.zsf.boot.office.excel.constant.*;
import com.zycfc.zsf.boot.exception.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import java.io.*;
import org.apache.poi.hssf.usermodel.*;
import java.math.*;
import java.util.*;
import org.slf4j.*;

public class ExcelExportService
{
    private static final Logger logger;
    private String excelTemplatePath;
    private String excelUploadPath;
    private String excelType;
    private int firstDataRow;
    
    public ExcelExportService() {
        this.excelTemplatePath = "D:\\experiment\\";
        this.excelUploadPath = "D:\\experiment\\upload\\";
        this.excelType = ExcelConstant.ExcelTypeEnum.EXCEL2007.getValue();
        this.firstDataRow = 1;
    }
    
    public ExcelExportResponse exportExcel(final String excelTemplateName, final String excelType, final List<List<Object>> exportDataList) {
        try {
            AssertUtils.isEmpty(excelTemplateName, ExcelConstant.ExcelExportErrorEnum.ERROR1002.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1002.getDisplayName());
            AssertUtils.isNull(ExcelConstant.ExcelTypeEnum.getNm(excelType), ExcelConstant.ExcelExportErrorEnum.ERROR1003.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1003.getDisplayName());
            return this.createExcelWorkBook(excelTemplateName, excelType, exportDataList, this.excelTemplatePath, this.firstDataRow);
        }
        catch (ServiceException se) {
            return new ExcelExportResponse(se.getMessage());
        }
        catch (Exception e) {
            ExcelExportService.logger.error("exportExcel:" + ExcelConstant.ExcelExportErrorEnum.ERROR1099.getValue(), (Throwable)e);
            return new ExcelExportResponse(ExcelConstant.ExcelExportErrorEnum.ERROR1099.getDisplayName());
        }
    }
    
    public ExcelExportResponse uploadExcel(final String excelTemplateName, final String excelType, final List<List<Object>> exportDataList, final String excelName) {
        try {
            AssertUtils.isEmpty(excelTemplateName, ExcelConstant.ExcelExportErrorEnum.ERROR1002.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1002.getDisplayName());
            AssertUtils.isNull(ExcelConstant.ExcelTypeEnum.getNm(excelType), ExcelConstant.ExcelExportErrorEnum.ERROR1003.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1003.getDisplayName());
            AssertUtils.isEmpty(excelName, ExcelConstant.ExcelExportErrorEnum.ERROR1004.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1004.getDisplayName());
            return this.uploadExcelFile(this.createExcelWorkBook(excelTemplateName, excelType, exportDataList, this.excelTemplatePath, this.firstDataRow).getRetWorkbook(), excelName, excelType, this.excelUploadPath);
        }
        catch (ServiceException se) {
            return new ExcelExportResponse(se.getMessage());
        }
        catch (Exception e) {
            ExcelExportService.logger.error("exportExcel:" + ExcelConstant.ExcelExportErrorEnum.ERROR1099.getValue(), (Throwable)e);
            return new ExcelExportResponse(ExcelConstant.ExcelExportErrorEnum.ERROR1099.getDisplayName());
        }
    }
    
    public ExcelExportResponse uploadExcel(final String excelTemplateName, final String excelType, final List<List<Object>> exportDataList, final String excelName, final String excelUploadPath) {
        try {
            AssertUtils.isEmpty(excelTemplateName, ExcelConstant.ExcelExportErrorEnum.ERROR1002.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1002.getDisplayName());
            AssertUtils.isNull(ExcelConstant.ExcelTypeEnum.getNm(excelType), ExcelConstant.ExcelExportErrorEnum.ERROR1003.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1003.getDisplayName());
            AssertUtils.isEmpty(excelName, ExcelConstant.ExcelExportErrorEnum.ERROR1004.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1004.getDisplayName());
            AssertUtils.isEmpty(excelUploadPath, ExcelConstant.ExcelExportErrorEnum.ERROR1005.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1005.getDisplayName());
            return this.uploadExcelFile(this.createExcelWorkBook(excelTemplateName, excelType, exportDataList, this.excelTemplatePath, this.firstDataRow).getRetWorkbook(), excelName, excelType, excelUploadPath);
        }
        catch (ServiceException se) {
            return new ExcelExportResponse(se.getMessage());
        }
        catch (Exception e) {
            ExcelExportService.logger.error("exportExcel:" + ExcelConstant.ExcelExportErrorEnum.ERROR1099.getValue(), (Throwable)e);
            return new ExcelExportResponse(ExcelConstant.ExcelExportErrorEnum.ERROR1099.getDisplayName());
        }
    }
    
    public ExcelExportResponse exportExcel(final String excelTemplateName, final String excelType, final List<List<Object>> exportDataList, final String excelTemplatePath, final int firstDataRow) {
        try {
            AssertUtils.isEmpty(excelTemplateName, ExcelConstant.ExcelExportErrorEnum.ERROR1002.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1002.getDisplayName());
            AssertUtils.isNull(ExcelConstant.ExcelTypeEnum.getNm(excelType), ExcelConstant.ExcelExportErrorEnum.ERROR1003.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1003.getDisplayName());
            AssertUtils.isEmpty(excelTemplatePath, ExcelConstant.ExcelExportErrorEnum.ERROR1007.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1007.getDisplayName());
            AssertUtils.isTrue(firstDataRow < 0, ExcelConstant.ExcelExportErrorEnum.ERROR1006.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1006.getDisplayName());
            return this.createExcelWorkBook(excelTemplateName, excelType, exportDataList, excelTemplatePath, firstDataRow);
        }
        catch (ServiceException se) {
            return new ExcelExportResponse(se.getMessage());
        }
        catch (Exception e) {
            ExcelExportService.logger.error("exportExcel:" + ExcelConstant.ExcelExportErrorEnum.ERROR1099.getValue(), (Throwable)e);
            return new ExcelExportResponse(ExcelConstant.ExcelExportErrorEnum.ERROR1099.getDisplayName());
        }
    }
    
    public ExcelExportResponse uploadExcel(final String excelTemplateName, final String excelType, final List<List<Object>> exportDataList, final String excelTemplatePath, final int firstDataRow, final String excelName, final String excelUploadPath) {
        try {
            AssertUtils.isEmpty(excelTemplateName, ExcelConstant.ExcelExportErrorEnum.ERROR1002.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1002.getDisplayName());
            AssertUtils.isNull(ExcelConstant.ExcelTypeEnum.getNm(excelType), ExcelConstant.ExcelExportErrorEnum.ERROR1003.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1003.getDisplayName());
            AssertUtils.isEmpty(excelTemplatePath, ExcelConstant.ExcelExportErrorEnum.ERROR1007.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1007.getDisplayName());
            AssertUtils.isTrue(firstDataRow < 0, ExcelConstant.ExcelExportErrorEnum.ERROR1006.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1006.getDisplayName());
            AssertUtils.isEmpty(excelName, ExcelConstant.ExcelExportErrorEnum.ERROR1004.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1004.getDisplayName());
            AssertUtils.isEmpty(excelUploadPath, ExcelConstant.ExcelExportErrorEnum.ERROR1005.getValue(), ExcelConstant.ExcelExportErrorEnum.ERROR1005.getDisplayName());
            return this.uploadExcelFile(this.createExcelWorkBook(excelTemplateName, excelType, exportDataList, excelTemplatePath, firstDataRow).getRetWorkbook(), excelName, excelType, excelUploadPath);
        }
        catch (ServiceException se) {
            return new ExcelExportResponse(se.getMessage());
        }
        catch (Exception e) {
            ExcelExportService.logger.error("exportExcel:" + ExcelConstant.ExcelExportErrorEnum.ERROR1099.getValue(), (Throwable)e);
            return new ExcelExportResponse(ExcelConstant.ExcelExportErrorEnum.ERROR1099.getDisplayName());
        }
    }
    
    private ExcelExportResponse createExcelWorkBook(final String excelTemplateName, final String excelType, final List<List<Object>> exportDataList, final String excelTemplatePath, final int firstDataRow) throws ServiceException {
        final String excelFilePath = excelTemplatePath + excelTemplateName + "." + excelType;
        final File excelFile = this.checkFileExists(excelFilePath);
        final Workbook workbook = this.getExcelWorkBook(excelFile, excelType);
        final CellStyle cellStyleOfDate = this.getCellStyle(workbook);
        final Sheet sheet = workbook.getSheetAt(0);
        if (exportDataList != null && !exportDataList.isEmpty()) {
            for (int i = 0; i < exportDataList.size(); ++i) {
                final Row dataRow = sheet.createRow(firstDataRow + i);
                final List<Object> instanceDataList = exportDataList.get(i);
                if (instanceDataList != null && !instanceDataList.isEmpty()) {
                    for (int j = 0; j < instanceDataList.size(); ++j) {
                        final Cell cell = dataRow.createCell(j);
                        final Object object = instanceDataList.get(j);
                        if (object != null) {
                            this.setCell(cell, object, cellStyleOfDate);
                        }
                    }
                }
            }
        }
        return new ExcelExportResponse(workbook);
    }
    
    private ExcelExportResponse uploadExcelFile(final Workbook workbook, final String excelName, final String excelType, final String excelUploadPath) throws ServiceException {
        FileOutputStream fo = null;
        BufferedOutputStream bo = null;
        final File outFile = new File(excelUploadPath);
        if (!outFile.exists()) {
            outFile.mkdir();
        }
        final String excelFilePath = excelUploadPath + excelName + "." + excelType;
        try {
            fo = new FileOutputStream(excelFilePath);
            bo = new BufferedOutputStream(fo);
            workbook.write((OutputStream)fo);
            fo.flush();
            bo.flush();
        }
        catch (FileNotFoundException e) {
            ExcelExportService.logger.error("uploadExcelFile:" + ExcelConstant.ExcelExportErrorEnum.ERROR1099.getValue(), (Throwable)e);
            throw new ServiceException((IExceptionCode)OfficeExceptionCode.ERROR1008);
        }
        catch (IOException e2) {
            ExcelExportService.logger.error("uploadExcelFile:" + ExcelConstant.ExcelExportErrorEnum.ERROR1099.getValue(), (Throwable)e2);
            throw new ServiceException((IExceptionCode)OfficeExceptionCode.ERROR1099);
        }
        finally {
            try {
                if (bo != null) {
                    bo.close();
                }
                if (fo != null) {
                    fo.close();
                }
            }
            catch (IOException e3) {
                ExcelExportService.logger.error("uploadExcelFile:" + ExcelConstant.ExcelExportErrorEnum.ERROR1099.getValue(), (Throwable)e3);
                throw new ServiceException((IExceptionCode)OfficeExceptionCode.ERROR1010);
            }
        }
        return new ExcelExportResponse(0, "", excelFilePath);
    }
    
    private CellStyle getCellStyle(final Workbook workbook) {
        final CellStyle cellStyle = workbook.createCellStyle();
        final CreationHelper createHelper = workbook.getCreationHelper();
        cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd hh:mm:ss"));
        return cellStyle;
    }
    
    private Workbook getExcelWorkBook(final File excelFile, final String excelType) throws ServiceException {
        try {
            Object o;
            if (ExcelConstant.ExcelTypeEnum.EXCEL2007.getValue().equals(excelType)) {
                final FileInputStream fileInputStream;
                o = new XSSFWorkbook((InputStream)fileInputStream);
                fileInputStream = new FileInputStream(excelFile);
            }
            else {
                final FileInputStream fileInputStream2;
                o = new HSSFWorkbook((InputStream)fileInputStream2);
                fileInputStream2 = new FileInputStream(excelFile);
            }
            return (Workbook)o;
        }
        catch (IOException e) {
            ExcelExportService.logger.error("getExcelWorkBook:" + ExcelConstant.ExcelExportErrorEnum.ERROR1001.getValue(), (Throwable)e);
            throw new ServiceException((IExceptionCode)OfficeExceptionCode.ERROR1001);
        }
    }
    
    private File checkFileExists(final String excelFilePath) throws ServiceException {
        final File file = new File(excelFilePath);
        if (file.exists()) {
            return file;
        }
        ExcelExportService.logger.error("checkFileExists:" + ExcelConstant.ExcelExportErrorEnum.ERROR1000.getValue());
        throw new ServiceException((IExceptionCode)OfficeExceptionCode.ERROR1000);
    }
    
    private void setCell(final Cell cell, final Object object, final CellStyle cellStyleOfDate) throws ServiceException {
        if (object instanceof String) {
            cell.setCellType(1);
            cell.setCellValue(object.toString());
        }
        else if (object instanceof Integer) {
            cell.setCellType(0);
            final int value = Integer.valueOf(object.toString());
            cell.setCellValue((double)value);
        }
        else if (object instanceof Long) {
            cell.setCellType(0);
            final long value2 = Long.valueOf(object.toString());
            cell.setCellValue((double)value2);
        }
        else if (object instanceof BigDecimal) {
            cell.setCellType(0);
            final BigDecimal value3 = new BigDecimal(object.toString());
            cell.setCellValue(value3.doubleValue());
        }
        else if (object instanceof Boolean) {
            cell.setCellType(4);
            final boolean value4 = new Boolean(object.toString());
            cell.setCellValue(value4);
        }
        else if (object instanceof Date) {
            cell.setCellStyle(cellStyleOfDate);
            final Date date = (Date)object;
            cell.setCellValue(date);
        }
        else {
            cell.setCellType(1);
            cell.setCellValue(object.toString());
        }
    }
    
    public String getExcelTemplatePath() {
        return this.excelTemplatePath;
    }
    
    public void setExcelTemplatePath(final String excelTemplatePath) {
        this.excelTemplatePath = excelTemplatePath;
    }
    
    public String getExcelUploadPath() {
        return this.excelUploadPath;
    }
    
    public void setExcelUploadPath(final String excelUploadPath) {
        this.excelUploadPath = excelUploadPath;
    }
    
    public String getExcelType() {
        return this.excelType;
    }
    
    public void setExcelType(final String excelType) {
        this.excelType = excelType;
    }
    
    public int getFirstDataRow() {
        return this.firstDataRow;
    }
    
    public void setFirstDataRow(final int firstDataRow) {
        this.firstDataRow = firstDataRow;
    }
    
    static {
        logger = LoggerFactory.getLogger((Class)ExcelExportService.class);
    }
}
